package zb.WX.service.userManage;

import com.google.gson.Gson;
import zb.WX.bean.userManage.UserList;
import zb.WX.bean.userManage.UserSingle;
import zb.WX.util.AccessTokenSaveUtil;
import zb.WX.util.GlobalVar;
import zb.WX.util.HttpsUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户管理
 *
 * @author Administrator
 */
public class ManageUserService {
    /*
     * 给用户打标签：
     * 	1.实际上就是给用户分组；
     *  2.openId需要自己传递；
     *  3.可以重复执行；
     *  4.在没给用户打标签之前，用户是没有标签的。
     */
    public String setTagForUser(String tagId, List<String> openid_list) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.SET_TAG_FANS.replace("ACCESS_TOKEN", access_token);

        UserList manage = new UserList();
        manage.setOpenid_list(openid_list);
        manage.setTagId(tagId);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(manage);
        System.out.println("粉丝打标签：" + jsonStr);

        String result = HttpsUtil.getHttpsToJson(urlStr, jsonStr);
        System.out.println("粉丝打标签结果：" + result);

        return result;
    }

    /*
     * 批量为用户取消标签：
     *
     */
    public String cancleTagForUser(String tagId, List<String> openid_list) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.CANCLE_TAG_FANS.replace("ACCESS_TOKEN", access_token);

        UserList manage = new UserList();
        manage.setOpenid_list(openid_list);
        manage.setTagId(tagId);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(manage);
        System.out.println("批量取消粉丝标签：" + jsonStr);

        String result = HttpsUtil.getHttpsToJson(urlStr, jsonStr);
        System.out.println("批量取消粉丝标签结果：" + result);

        return result;
    }

    /*
     * 查询用户属于哪一个标签：
     * 1.返回结果如下格式：{"tagid_list":[104,107]}
     * 2.一个用户可以打多个标签
     *
     */
    public String queryTagWithUser(String openId) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();
        String urlStr = GlobalVar.QUERY_TAG_WITH_USER.replace("ACCESS_TOKEN", access_token);

        UserSingle userSingle = new UserSingle();
        userSingle.setOpenid(openId);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(userSingle);
        System.out.println("查询单个用户标签：" + jsonStr);

        String result = HttpsUtil.getHttpsToJson(urlStr, jsonStr);
        System.out.println("查询单个用户标签结果：" + result);
        return result;

    }

    /*
     * 设置用户备注名：
     *
     */
    public String setFansRemark(String openId, String remark) {
        String access_token = AccessTokenSaveUtil.getLocationAccesstonken();

        String urlStr = GlobalVar.SET_FANS_REMARK.replace("ACCESS_TOKEN", access_token);
        UserSingle userSingle = new UserSingle();
        userSingle.setOpenid(openId);
        userSingle.setRemark(remark);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(userSingle);
        System.out.println("修改粉丝" + openId + "备注为：" + jsonStr);

        String result = HttpsUtil.getHttpsToJson(urlStr, jsonStr);
        System.out.println("修改备注结果：" + result);

        return result;
    }


    public static void main(String[] args) {
        //给以下用户打标签
        List<String> list = new ArrayList<String>();
        list.add("oHvUE1l4ZneIiDf6BRa9SINR5XJc");

        new ManageUserService().setTagForUser("101", list);

		/*List<String> list1 = new ArrayList<String>();
		list1.add("o2joUxKYEB0Tg-pcUEyCaglOhnYk");
		new ManageUserService().setTagForUser("106", list1);*/

		
		/*List<String> list = new ArrayList<String>();
	
		list.add("oHvUE1l4ZneIiDf6BRa9SINR5XJc");
		new ManageUserService().cancleTagForUser("101", list);*/


        //查询单个用户身上的标签列表
        new ManageUserService().queryTagWithUser("oHvUE1l4ZneIiDf6BRa9SINR5XJc");


        //修改用户备注
        //new ManageUserService().setFansRemark("o2joUxKYEB0Tg-pcUEyCaglOhnYk", "亲爱的");


    }
}
